package ops.weixin.cp.util;

import java.lang.reflect.Array;

/**
 * 
 * @author SuM
 * @date: 2019年12月25日上午10:31:39
 */
public class StringUtils {
    public static final String EMPTY = "";
	

    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }
    
    public static boolean isNotBlank(final CharSequence cs) {
        return !isBlank(cs);
    }
    
    public static boolean isAnyBlank(final CharSequence... css) {
        if (isEmpty(css)) {
          return false;
        }
        for (final CharSequence cs : css) {
          if (isBlank(cs)) {
            return true;
          }
        }
        return false;
      }
    
    public static boolean isEmpty(final CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    public static boolean isNotEmpty(final CharSequence cs) {
        return !isEmpty(cs);
    }
    
    public static String trimToEmpty(final String str) {
        return str == null ? EMPTY : str.trim();
    }
    
    public static String trimToNull(final String str) {
        final String ts = trim(str);
        return isEmpty(ts) ? null : ts;
    }

    public static String trim(final String str) {
        return str == null ? null : str.trim();
    }

    public static <T extends CharSequence> T defaultIfBlank(final T str, final T defaultStr) {
        return isBlank(str) ? defaultStr : str;
    }

    public static String replaceAll(final String text, final String regex, final String replacement) {
    	 if (text == null || regex == null || replacement == null) {
             return text;
         }
         return text.replaceAll(regex, replacement);
    }
    
    public static boolean contains(final CharSequence seq, final CharSequence searchSeq) {
        if (seq == null || searchSeq == null) {
            return false;
        }
        return indexOf(seq, searchSeq, 0) >= 0;
    }
    
    public static  int indexOf(final CharSequence cs, final CharSequence searchChar, final int start) {
        return cs.toString().indexOf(searchChar.toString(), start);
    }
    
    public static boolean isAnyEmpty(final CharSequence... css) {
        if (isEmpty(css)) {
          return false;
        }
        for (final CharSequence cs : css) {
          if (isEmpty(cs)) {
            return true;
          }
        }
        return false;
      }
    
    public static boolean isEmpty(final Object[] array) {
        return getLength(array) == 0;
    }

    public static int getLength(final Object array) {
        if (array == null) {
            return 0;
        }
        return Array.getLength(array);
    }

}
